//
//  EditCarDetailViewController.m
//  CarTown
//
//  Created by xionghao on 15/9/9.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "EditCarDetailViewController.h"
#import "CarbrandModel.h"
#import "CarDetail.h"
#import "CarprogChildmode.h"
#import "Puser.h"
@interface EditCarDetailViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textfield;
@property (nonatomic,strong) Puser *user;
@end

@implementation EditCarDetailViewController
+(instancetype)viewcontroller{
    EditCarDetailViewController *vc=[[EditCarDetailViewController alloc]initWithNibName:NSStringFromClass([self class]) bundle:nil];
    return vc;
}
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title=@"自定义车型";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"确认" style:UIBarButtonItemStyleDone target:self action:@selector(saveclick)];
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
-(void)saveclick{
    if (self.textfield.text.length>0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"正在添加..";
        
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
               NSMutableDictionary *params=[NSMutableDictionary dictionary];
        params[@"cat_id"]=self.child.cat_id;
        params[@"type_id"]=self.type;
        NSInteger power=[self.user.power integerValue];
        if (power==1) {
            params[@"user_id"]=self.user.user_id;
        }else if (power==5){
            params[@"com_id"]=self.user.com_id;
        }else if (power==8){
            params[@"parent_id"]=self.user.parent_id;
            params[@"com_id"]=self.user.com_id;
        }
        params[@"content"]=self.textfield.text;
        
        [PostUitls Post:addcardetail parameters:params success:^(id response) {
            hud.mode = MBProgressHUDModeText;
           
            NSLog(@"%@",response);
            NSDictionary *dict=response;
            NSNumber *state=dict[@"state"];
            if ([state integerValue]==400) {
                NSNumber *IDnumber=dict[@"data"];
                CarDetail *detail=[[CarDetail alloc]init];
                detail.ID=[NSString stringWithFormat:@"%ld",[IDnumber integerValue]];
                detail.name=self.textfield.text;
                 hud.labelText=@"添加自定义车型成功";
                       if ([_delegate respondsToSelector:@selector(eidtViewControllerClick:)]) {
                           [_delegate eidtViewControllerClick:detail];
                        }
                
            }else{
                hud.labelText=@"添加自定义车型失败";
            }
            
            [hud hide:YES afterDelay:1];

        } failure:^(NSError *error) {
            hud.mode = MBProgressHUDModeText;
            hud.labelText=@"网络异常";
            [hud hide:YES afterDelay:1];
        }];
        

        
    }else{
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"自定义车型不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
